﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGame.Networking.Clientside.ServerClientPackets
{
    class TeleportEntityPacket : ClientPacket
    {

        private GameEntity Ent;
        private int X;
        private int Y;
        private int Angle;

        public override ServerClientNet GetID()
        {
            return ServerClientNet.EntityTeleport;
        }

        public override bool Validate(Lidgren.Network.NetIncomingMessage msg)
        {
            string id = msg.ReadString();
            this.X = msg.ReadInt32();
            this.Y = msg.ReadInt32();
            this.Angle = msg.ReadInt32();

            if (!this.ValidateString(id) || !Singletons.NetworkEntity.IsValid(id))
            {
                return false;
            }

            this.Ent = Singletons.NetworkEntity.GetEntity(id);

            if (!this.ValidateInt(this.X))
            {
                return false;
            }

            if (!this.ValidateInt(this.Y))
            {
                return false;
            }

            if (!this.ValidateInt(this.Angle))
            {
                return false;
            }

            return true;
        }

        public override void Handle()
        {
            this.Ent.Move(this.X, this.Y, this.Angle);
        }

        public override void Handle(Lidgren.Network.NetIncomingMessage message)
        {
            this.Validate(message);
            this.Handle();
        }
    }
}
